package com.game.autoTool.project.web.common;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * 使用 JFrame 创建一个带有切换选项卡功能且可更换图标的窗口
 */
public class JFrameExample2 {
    public static void main(String[] args) {
        // 创建 JFrame 对象
        JFrame frame = new JFrame("乌鱼助手");

        // 设置窗口关闭操作
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 加载图标
        try {
            //需要绝对路径
            BufferedImage icon = ImageIO.read(new File("E:\\bjdl\\autoTool\\src\\main\\resources\\imageTemplate\\icon_trophy.png"));
            frame.setIconImage(icon);
        } catch (IOException e) {
            System.err.println("无法加载图标: " + e.getMessage());
        }

        // 创建一个边界布局
        frame.setLayout(new BorderLayout());

        // 创建一个面板用于放置按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));

        // 创建多个 JPanel 用于模拟不同的 "tab" 内容
        JPanel tab1 = new JPanel();
        tab1.add(new JLabel("This is Tab 1"));
        JPanel tab2 = new JPanel();
        tab2.add(new JLabel("This is Tab 2"));
        JPanel tab3 = new JPanel();
        tab3.add(new JLabel("This is Tab 3"));

        // 创建一个面板用于显示当前激活的 tab 内容
        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.add(tab1);

        // 创建按钮并添加到按钮面板
        JButton button1 = new JButton("Tab 1");
        JButton button2 = new JButton("Tab 2");
        JButton button3 = new JButton("Tab 3");

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contentPanel.removeAll();
                contentPanel.add(tab1);
                contentPanel.revalidate();
                contentPanel.repaint();
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contentPanel.removeAll();
                contentPanel.add(tab2);
                contentPanel.revalidate();
                contentPanel.repaint();
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contentPanel.removeAll();
                contentPanel.add(tab3);
                contentPanel.revalidate();
                contentPanel.repaint();
            }
        });

        buttonPanel.add(button1);
        buttonPanel.add(button2);
        buttonPanel.add(button3);

        // 将按钮面板添加到窗口左侧
        frame.add(buttonPanel, BorderLayout.WEST);
        // 将内容面板添加到窗口中间
        frame.add(contentPanel, BorderLayout.CENTER);

        // 设置窗口大小
        frame.setSize(800, 600);

        // 使窗口居中显示
        frame.setLocationRelativeTo(null);

        // 显示窗口
        frame.setVisible(true);
    }
}    